package pkg.andru.widget;

import pkg.andru.annotation.Alpha;

import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;

/**
 * absolute translate animation, only support absolute translate
 * 
 * @author idiottiger
 * @hide
 */
@Alpha
class AbsoluteTranslateAnimation extends Animation {

    private float mFromXDelta;
    private float mToXDelta;
    private float mFromYDelta;
    private float mToYDelta;

    private float preTx, preTy;

    private Transformation mTransformation;

    /**
     * need pass from x, from y, to x, to y, all coordinate is absoluted
     * 
     * @param fromXDelta
     * @param toXDelta
     * @param fromYDelta
     * @param toYDelta
     */
    public AbsoluteTranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
        mTransformation = new Transformation();
        setTransformation(fromXDelta, toXDelta, fromYDelta, toYDelta);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        float dx = 0;
        float dy = 0;
        if (mFromXDelta != mToXDelta) {
            dx = ((mToXDelta - mFromXDelta) * interpolatedTime);
        }
        if (mFromYDelta != mToYDelta) {
            dy = ((mToYDelta - mFromYDelta) * interpolatedTime);
        }
        t.getMatrix().setTranslate(dx, dy);
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    /**
     * return the current time transformation matrix, current time use
     * {@link AnimationUtils#currentAnimationTimeMillis()}
     * 
     * @return
     */
    public Matrix getCurrentTimeTransformationMatrix() {
        return getTransformationMatrix(AnimationUtils.currentAnimationTimeMillis());
    }

    /**
     * return given time transformation matrix
     * 
     * @param currentTime
     * @return
     */
    public Matrix getTransformationMatrix(long currentTime) {
        mTransformation.clear();
        getTransformation(currentTime, mTransformation);
        return mTransformation.getMatrix();
    }

    /**
     * set transformation, if you want repeat use this animation, you can
     * {@link #reset()} and
     * {@link #setTransformation(float, float, float, float)} again.
     * 
     * @param fromXDelta
     * @param toXDelta
     * @param fromYDelta
     * @param toYDelta
     */
    public void setTransformation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
        mFromXDelta = fromXDelta;
        mToXDelta = toXDelta;
        mFromYDelta = fromYDelta;
        mToYDelta = toYDelta;
    }

    public void reset() {
        super.reset();
        mFromXDelta = 0;
        mFromYDelta = 0;
        mToXDelta = 0;
        mToYDelta = 0;
        mTransformation.clear();
        preTx = 0;
        preTy = 0;
    }

    /**
     * get current post translate value
     * 
     * @param outVaules
     */
    void getCurrentPostTranslate(float[] outVaules) {
        Matrix m = getCurrentTimeTransformationMatrix();

        float[] values = new float[9];
        m.getValues(values);

        float tx = values[2] - preTx;
        float ty = values[5] - preTy;

        preTx = values[2];
        preTy = values[5];

        outVaules[0] = tx;
        outVaules[1] = ty;
    }

}